package fragment;

import android.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.customview.R;

import java.io.File;
import java.util.List;

import adapter.FileManagerVideoListAdapter;
import moudle.FileManagerMoudle;
import view.TranslateView;

/**
 * Created by eyesee on 2017/4/22.
 */

public class FileManagerVideoList extends Fragment implements AdapterView.OnItemSelectedListener, View.OnClickListener, AdapterView.OnItemClickListener{

    public static final String TAG="fileManagerFragment";


    private View mView;
    private ListView mListView;
    private TranslateView mBorder;
    private ImageButton mButton1;
    private MenuFragment mMenuFragment;
    private FileManagerMoudle mMoudle;
    private ImageView mAnimImg;

    private int[] loadId = {R.drawable.load01,R.drawable.load02,R.drawable.load03,R.drawable.load04,R.drawable.load05,R.drawable.load06,R.drawable.load07};
    private AnimationDrawable mAnimat;
    private ImageView mFog;
    private int mItemHeight;
    private List<File> mList;
    private File mFile;
    private FileManagerVideoListAdapter mAdapter;
    private MenuFragmentTwo mMenuFragmentTwo;
    private SortListener mSortListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(this.getActivity(), R.layout.filemanager_videolist, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        initView(mView);
        setData();

        return mView;
    }



    private void setData() {
        mMoudle = new FileManagerMoudle();
        String dir = mMoudle.getDir();          //获取内部存储和外部存储路径以\n拼接起来的字符串
        if (!dir.equals("")){
            String[] split = dir.split("\n");   //字符串拆分成单个完整的路径
            if (split!=null){
                for (String path : split) {
                    if (!path.equals("")){
                        //扫描文件
                        mFile = new File(path);
                        mList = mMoudle.FileScan(mFile);
                    }
                }
            }
        }

        mAdapter = new FileManagerVideoListAdapter(getActivity(), mList);
        mItemHeight = mAdapter.getItemHeight();
        mListView.setAdapter(mAdapter);

        mButton1.setOnClickListener(this);

        mListView.setOnItemSelectedListener(this);
        mListView.setOnItemClickListener(this);


    }

    private void initView(View view) {
        mMenuFragment = new MenuFragment();
        mMenuFragmentTwo = new MenuFragmentTwo();

        mFog = (ImageView) view.findViewById(R.id.fog);
        mAnimImg = (ImageView) view.findViewById(R.id.animat);
        mButton1 = (ImageButton) view.findViewById(R.id.imgbt);
        mListView = (ListView) view.findViewById(R.id.filemanager_listview);
        mBorder = (TranslateView) view.findViewById(R.id.border);
        mBorder.setTvScreen(true);
    }


    @Override
    public void onClick(View v) {
        //按菜单键弹出
        mMenuFragment.show(getFragmentManager(),"");
        //加载动画
//        FrameAnimat(mAnimImg);
    }

    //listview的item的选择事件
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mBorder.setVisibility(View.VISIBLE);
        mBorder.setSelectView(view);

        //当前选中item为屏幕中可见的倒数第二个item时,
        // 继续向下选择,焦点位置不变,listview向上滑动一个item的距离
        int index = mListView.getLastVisiblePosition() - 1;
        mListView.smoothScrollToPosition(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    //listview的item的点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "点击了"+position, Toast.LENGTH_SHORT).show();
    }




   /**时间排序,使用接口对象调用接口中方法*/
    public void TimeSortListener(){
        mSortListener.TimeSort(mListView,mMoudle,mFile);
    }

    /**名称排序,使用接口对象调用接口中方法*/
    public void NameSortListener(){
        mSortListener.NameSort(mListView,mMoudle,mFile);
    }

    //声明方法,传入接口对象
    public void SetSortListener(SortListener sortListener){
        mSortListener = sortListener;
    }



    //声明接口
    public interface SortListener{
        void TimeSort(ListView listView,FileManagerMoudle fileManagerMoudle , File file);
        void NameSort(ListView listView,FileManagerMoudle fileManagerMoudle , File file);
    }





    public void FrameAnimat(View view){
        view.setVisibility(View.VISIBLE);
        mAnimat = new AnimationDrawable();
        for (int i = 0; i < loadId.length; i++) {
            int id = loadId[i];
            Drawable drawable1 = getResources().getDrawable(id);
            mAnimat.addFrame(drawable1,100);
        }
        mAnimat.setOneShot(false);
        view.setBackgroundDrawable(mAnimat);
        mAnimat.start();

    }
}
